import { AxiosRequestConfig, AxiosResponse } from 'axios/index';
import { authedApi } from 'src/utils/axios';
import { CurrentDataset, UploadList } from 'stores/platform-upload';

export const getPresignedUrlsReq = (
  dataset: CurrentDataset,
  upload_id: string
): PresignedUrlReq => {
  return {
    dataset_id: dataset.id,
    version_id: dataset.version_id,
    project_id: dataset.project_id,
    upload_id: upload_id,
  };
};

export const getDatasetReq = (
  dataset: CurrentDataset,
  upload_id: string
): PresignedUrlReq => {
  return {
    dataset_id: dataset.id,
    version_id: dataset.version_id,
    project_id: dataset.project_id,
    upload_id: upload_id,
  };
};

export const getUploadReq = (
  dataset: CurrentDataset,
  identifier: string,
  size: number,
  name: string,
  chunkSize: number
): UploadIdReq => {
  return {
    dataset_id: dataset.id,
    version_id: dataset.version_id,
    project_id: dataset.project_id,
    identifier: identifier,
    total_size: size,
    row_name: name,
    chunk_size: chunkSize,
  };
};

export const getImportReq = (
  dataset: CurrentDataset,
  filename: string
): ImportFileReq => {
  return {
    dataset_id: dataset.id,
    version_id: dataset.version_id,
    project_id: dataset.project_id,
    data: {
      filename,
    },
  };
};

export const getSuccessReq = (dataset: CurrentDataset) => {
  return {
    dataset_id: dataset.id,
    version_id: dataset.version_id,
    project_id: dataset.project_id,
  };
};

/* 获取切片上传的 url */
export async function getPresignedUrls(
  req: PresignedUrlReq,
  config?: AxiosRequestConfig
) {
  return authedApi.post<any, AxiosResponse<PresignedUrlsResponse>>(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/presigned_urls/`,
    {
      upload_id: req.upload_id,
    },
    {
      ...config,
    }
  );
}

/* 获取 minio upload id */
export async function getUploadId(
  req: UploadIdReq,
  config?: AxiosRequestConfig
) {
  return authedApi.post<any, AxiosResponse<UploadIdResponse>>(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/upload_task/`,
    {
      row_name: req.row_name,
      chunk_size: req.chunk_size,
      total_size: req.total_size,
      identifier: req.identifier,
    },
    {
      ...config,
    }
  );
}

//创建数据集 start
export async function createDataset(
  project_id: string,
  params: createDatasetReq
) {
  return authedApi.post<any, AxiosResponse<CreateDatasetResponse>>(
    `v2/projects/${project_id}/datasets/`,
    params
  );
}

export async function mergeChunks(
  req: PresignedUrlReq,
  config?: AxiosRequestConfig
) {
  return authedApi.post(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/merge_chunks/`,
    { upload_id: req.upload_id },
    {
      ...config,
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }
  );
}

export async function importFile(
  req: ImportFileReq,
  config?: AxiosRequestConfig
) {
  return authedApi.post(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/import_file/`,
    req.data,
    {
      ...config,
    }
  );
}

export async function createdSuccessfully(
  req: CreateSuccessfully,
  config?: AxiosRequestConfig
) {
  return authedApi.get(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/create_successfully/`,
    {
      ...config,
    }
  );
}

export async function getUploadList() {
  return authedApi.get<UploadList[]>('v2/upload_list/');
}

interface uploadInfoResponse {
  ids: string[];
}

export async function getChunkUploadedInfo(req: PresignedUrlReq) {
  return authedApi.post<any, AxiosResponse<uploadInfoResponse>>(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/chunk_info/`,
    { upload_id: req.upload_id }
  );
}

export async function updateStatus(req: PresignedUrlReq) {
  return authedApi.patch(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/update_status/`,
    { upload_id: req.upload_id }
  );
}

export async function updateImporting(req: CreateSuccessfully) {
  return authedApi.patch(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/update_importing/`
  );
}

export async function getFileId(req: PresignedUrlReq) {
  return authedApi.post<UploadList>(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/file_id/`,
    { upload_id: req.upload_id }
  );
}

export async function errorCleanUnUpload(dataset_id: number, action?: string) {
  return authedApi.patch(`v2/upload_error/`, { dataset_id, action });
}

export async function errorDeleteUnUpload(dataset_id: number) {
  return authedApi.patch(`v2/upload_del/`, { dataset_id });
}

export async function updateErrorStatus(req: CreateActionSuccessfully) {
  return authedApi.patch(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/update_fail_status/`,
    { action: req.action }
  );
}

export async function getSingleDataset(req: {
  project_id: string;
  dataset_id: string;
}) {
  return authedApi.get(
    `v2/projects/${req.project_id}/datasets/${req.dataset_id}/`
  );
}
interface UpdateItemStatus extends CreateSuccessfully {
  id: number;
  status: string;
}
export async function updateItemStatus(req: UpdateItemStatus) {
  return authedApi.post<UploadList[]>(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/${req.id}/update_item_status/`,
    {
      error_status: req.status,
    }
  );
}

export async function getUnCompleteFileList(req: CreateSuccessfully) {
  return authedApi.get<UploadList[]>(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/uploaded_lists/`
  );
}

export async function getImportedFiles(req: CreateSuccessfully) {
  return authedApi.get<{ names: string[] }>(
    `v3/projects/${req.project_id}/datasets/${req.dataset_id}/version/${req.version_id}/minio/imported_files/`
  );
}

interface CreateDatasetResponse {
  dataset_id: number;
  version_id: number;
}

export interface UploadIdResponse {
  upload_id: string;
}

export interface PresignedUrlsResponse {
  urls: string[];
}

export interface BaseUploadReq {
  project_id: string;
  dataset_id: number;
  version_id: number;
}

export interface UploadIdReq extends BaseUploadReq {
  row_name: string;
  chunk_size: number;
  total_size: number;
  identifier: string;
}

export interface PresignedUrlReq extends BaseUploadReq {
  upload_id: string;
}
export interface CreateSuccessfully {
  project_id: string;
  dataset_id: number;
  version_id: number;
}
export interface CreateActionSuccessfully extends CreateSuccessfully {
  action: string;
}

export interface ImportFileReq extends BaseUploadReq {
  data: {
    filename: string;
  };
}

interface createDatasetReq {
  title: string;
  description?: string;
  dataset_type: string;
  version: string;
}
